package com.my.myrecorder.NXTest;

import android.app.Service;
import android.content.Intent;
import android.media.MediaRecorder;
import android.os.Environment;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;

/**
 * Created by 张英杰 on 2016/10/12.
 */
public class RecorderService extends Service{
    private MediaRecorder recorder;
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
//        * 第一：获取电话服务管理器TelephonyManager manager = this.getSystemService(TELEPHONY_SERVICE);
//        * 第二：通过TelephonyManager注册我们要监听的电话状态改变事件。manager.listen(new MyPhoneStateListener(),
//                *       PhoneStateListener.LISTEN_CALL_STATE);这里的PhoneStateListener.LISTEN_CALL_STATE就是我们想要
//                *      监听的状态改变事件，初次之外，还有很多其他事件哦。
//        * 第三步：通过extends PhoneStateListener来定制自己的规则。将其对象传递给第二步作为参数
//                * 第四步：这一步很重要，那就是给应用添加权限。android.permission.READ_PHONE_STATE


        // 拿到电话管理器
        TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
        // 监听电话状态
        // events:决定PhoneStateListener侦听什么内容
        tm.listen(new MyListener(), PhoneStateListener.LISTEN_CALL_STATE);
    }

    class MyListener extends PhoneStateListener {
        @Override
        public void onCallStateChanged(int state, String incomingNumber) {
            super.onCallStateChanged(state, incomingNumber);
            switch (state) {
                case TelephonyManager.CALL_STATE_IDLE:
                    System.out.println("空闲");
                    Log.i("phoneState=====","空闲");
                    if (recorder != null) {
                        recorder.stop();
                        recorder.release();
                        recorder = null;
                    }
                    break;
                case TelephonyManager.CALL_STATE_RINGING:
                    System.out.println("响铃");
                    Log.i("phoneState=====","响铃");
                    break;
                case TelephonyManager.CALL_STATE_OFFHOOK:
                    System.out.println("摘机");
                    Log.i("phoneState=====","摘机");;
                    String timeString = System.currentTimeMillis() + "";
                    String fileString = timeString.concat(".3gp");
                    // 开始录音
                    recorder = new MediaRecorder();
                    //VOICE_CALL：UPLINK + DOWNLINK
                    //MIC：自己麦克风的声音
                    //VOICE_DOWNLINK 对方的声音
                    //VOICE_UPLINK 自己的声音
                    recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
                    recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);

                    recorder.setOutputFile(Environment
                            .getExternalStorageDirectory() + "/" + fileString);
                    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
                    try {
                        recorder.prepare();
                        recorder.start();
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    break;
            }
        }
    }
}
